<template>
  <div class="singer" ref="singer">
    <list-view @select="selectSinger" :datas="singers" ref="list"></list-view>
    <router-view></router-view>
  </div>
</template>

<script type="text/ecmascript-6">
  import ListView from 'base/listview/listview'
  import {getSingerList} from 'api/singer'
  import {ERR_OK} from 'api/config'
  import Singer from 'common/js/singer'
  import {mapMutations} from 'vuex'
  const HOT_SINGER_LEN = 10
  const HOT_NAME = '热门'
  export default {
    name: 'Singer',
    components: {
      ListView
    },
    data() {
      return {
        singers: []
      }
    },
    created() {
      this._getSingerList()
    },
    methods: {
      //  选中歌手跳转到歌手详情页
      selectSinger(singer) {
        this.$router.push({
          path: `/singer/${singer.id}`
        })
        console.log('------singer',singer)
        this.setSinger(singer)
      },
      //  获取歌手数据列表。。jsop
      _getSingerList() {
        getSingerList().then((res) => {
          console.log('singers_getSingerList_res',res)
          if (res.code === ERR_OK) {
            //  对数据进行打包
            this.singers = this._normalizeSinger(res.data.list)
          }
        })
      },
      // 格式化歌手列表爬取过来的数据
      _normalizeSinger(list) {
            let map = {
              hot: {
                title: HOT_NAME,
                //  歌手数据
                items: []
              }
            }
            list.forEach((item, index) => {
              if (index < HOT_SINGER_LEN) {
                // 对歌手数据进行拼接。。图片地址是合成的
                map.hot.items.push(new Singer({
                  name: item.Fsinger_name,
                  id: item.Fsinger_mid
                }))
              }
              const key = item.Findex
              if (!map[key]) {
                map[key] = {
                  title: key,
                  items: []
                }
              }
              map[key].items.push(new Singer({
                name: item.Fsinger_name,
                id: item.Fsinger_mid
              }))
            })
            // 为了得到有序列表，我们需要处理 map
            let ret = []
            let hot = []
            for (let key in map) {
              let val = map[key]
              if (val.title.match(/[a-zA-Z]/)) {
                ret.push(val)
              } else if (val.title === HOT_NAME) {
                hot.push(val)
              }
            }
            // 歌手按照字母表的排序，整合
            // console.log('ret',ret)
            //  十个热门
            // console.log('hot',hot)
            //  对数据进行排序。。函数式编程
            ret.sort((a, b) => {
              return a.title.charCodeAt(0) - b.title.charCodeAt(0)
            })
            // console.log('hot.concat(ret)',JSON.stringify(hot.concat(ret)))
            return hot.concat(ret)
      },
      ...mapMutations({
        //  这是给这个东西取方法别名，给本地调用
        setSinger: 'SET_SINGER'
      })
    },
  }
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  .singer
    position relative
    top 0
    left 0
    height 80%
    overflow hidden
</style>
